import config from '../config';

const createGroup = (params, success, fail) => {
  dd.httpRequest({
    url: config.domain + '/class/base/createClassGroup',
    headers: {
      'Content-Type': 'application/json',
    },
    method: 'POST',
    dataType: 'json',
    data: JSON.stringify(params),

    success: (res) => {
      if (res.data.code == 0) {
        success && success(res);
      } else {
        fail && fail(res.data.message);
      }
    },

    fail: () => {
      fail && fail('网络异常');
    },
  });
};

const exportStudents = (params, success, fail) => {
  const searchParams = `?indexId=${params.classIds.join('&indexId=')}&uid=${
    params.dingTalkUserId
  }`;
  const url = config.domain + '/class/base/getExport' + `${searchParams}`;

  dd.saveFileToDingTalk({
    url, // 文件在第三方服务器地址
    name: `${params.classIds.join(',')}班.xls`,
    success: (res) => {
      /* data结构
         {"data":
            [
            {
            "spaceId": "" //空间id
            "fileId": "", //文件id
            "fileName": "", //文件名
            "fileSize": 111111, //文件大小
            "fileType": "", //文件类型
            }
            ]
         }
         */
    },
    fail: (err) => {
      dd.alert({
        content: JSON.stringify(err),
      });
    },
  });
};

module.exports = {
  createGroup,
  exportStudents,
};
